package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.Merchant;
import com.turing.post.organization.util.MerchantDao;
import com.turing.post.organization.util.MerchantSettDetail;
import com.turing.post.organization.util.MerchantSettDetailBusiness;
import com.turing.post.organization.util.MerchantSettDetailDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

@Stateless(mappedName = "organization.impl.MerchantSettDetailBusinessImpl")
@Remote
@Local
public class MerchantSettDetailBusinessImpl implements MerchantSettDetailBusiness {
	
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;
	
	@EJB(mappedName = "organization.impl.MerchantSettDetailDaoImpl")
	private MerchantSettDetailDao merchantSettDetailDaoImpl;
	

	@EJB(mappedName = "organization.impl.MerchantDaoImpl")
	private MerchantDao merchantDaoImpl;
	

	@Override
	public void addMerchantSettDetail(PCPSWebInput input,
			MerchantSettDetail entity) throws PCPSApplicationException {
		if(input == null || input.getOperId() == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "用户未登录或登录失效！");
		}
		MerchantSettDetail obj = new MerchantSettDetail();
		obj.setOperId(input.getOperId());
		//商户号
		if(PCPSUtil.isEmpty(entity.getMerchantNo())){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请选择商户！");
		}
		Merchant merchant = merchantDaoImpl.getMerchantForUpdate(entity.getMerchantNo());
		if(merchant == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "商户不存在！");
		}
		obj.setMerchantNo(entity.getMerchantNo());
		//发卡商号
		if(PCPSUtil.isEmpty(input.getOperBranchNo())){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "用户机构号为空！");
		}
		obj.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(input));
		//结算日期
		IssueCompany ic = issueCompanyDaoImpl.getIssueCompanyByIssueCompanyNo(PCPSUtil.getIssueCompanyNo(input));
		obj.setTranDate(ic.getTranDate());
		//结算时间
		obj.setTranTime(commonDaoImpl.getDatabaseSysTime());
		//结算类型
		if(PCPSUtil.isEmpty(entity.getTranType())){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请选择结算类型！");
		}
		obj.setTranType(entity.getTranType());
		//结算资金
		if(entity.getAmount() == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请输入结算资金！");
		}
		obj.setAmount(entity.getAmount());
		//银行账户
		if(entity.getPaidBankAccountId() == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请选择付款银行账户！");
		}
		obj.setPaidBankAccountId(entity.getPaidBankAccountId());
		//凭证号
		if(PCPSUtil.isEmpty(entity.getVoucherNo())){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请输入凭证号！");
		}
		obj.setVoucherNo(entity.getVoucherNo());
		obj.setRemark(entity.getRemark());
		
		obj.setMrchSettDetailId(commonDaoImpl.getUniqueIntegerKey());
		

		if(obj.getTranType().equals(MerchantSettDetail.MERCHANTSETTDETAIL_TRANTYPE_SETTLEMENT)){
			merchant.setPrepaidBalance(merchant.getPrepaidBalance().subtract(obj.getAmount()));
		}else{
			merchant.setPrepaidBalance(merchant.getPrepaidBalance().add(obj.getAmount()));
		}
		obj.setPrepaidBalance(merchant.getPrepaidBalance());
		//修改商户保证金
		merchantDaoImpl.updateMerchant(merchant);
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(input,
				SysOperLog.operType_Edit, "修改商户保证金", "merchantNo: " + merchant.getMerchantNo() + ";GuaranteeAmount:" + merchant.getPrepaidBalance());
		
		//新增商户结算明细
		merchantSettDetailDaoImpl.addMerchantSettDetail(obj);
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(input,
				SysOperLog.operType_Add, "新增商户结算明细", "merchantNo: " + obj.getMerchantNo() + ";tranType:" + obj.getTranType() + ";amount:" + obj.getAmount());
		
	}

	@Override
	public void delMerchantSettDetail(PCPSWebInput input,
			BigInteger mrchSettDetailId) throws PCPSApplicationException {
		if(input == null || input.getOperId() == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "用户未登录或登录失效！");
		}
		MerchantSettDetail msd = merchantSettDetailDaoImpl.getMerchantSettDetailByID(mrchSettDetailId);
		if(msd == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "要删除的商户结算明细信息不存在！");
		}
		merchantSettDetailDaoImpl.delMerchantSettDetail(msd);
		
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(input,
				SysOperLog.operType_Delete, "删除商户结算明细", "merchantNo: " + msd.getMerchantNo() + ";tranType:" + msd.getTranType() + ";amount:" + msd.getAmount());
	}

	@Override
	public MerchantSettDetail getMerchantSettDetailByID(PCPSWebInput input,
			BigInteger mrchSettDetailId) {
		return merchantSettDetailDaoImpl.getMerchantSettDetailByID(mrchSettDetailId);
	}

	@Override
	public List<MerchantSettDetail> getMerchantSettDetailList(
			PCPSWebInput input, String merchantNo, String starDate, String endDate,
			String voucherNo, Integer page) {
		return merchantSettDetailDaoImpl.getMerchantSettDetailList(input, merchantNo, starDate, endDate, voucherNo, page);
	}

	@Override
	public BigInteger getMerchantSettDetailTotal(PCPSWebInput input,
			String merchantNo, String starDate, String endDate, String voucherNo) {
		return merchantSettDetailDaoImpl.getMerchantSettDetailTotal(input, merchantNo, starDate, endDate, voucherNo);
	}

	@Override
	public void modifyMerchantSettDetail(PCPSWebInput input,
			MerchantSettDetail entity) throws PCPSApplicationException {
		if(input == null || input.getOperId() == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "用户未登录或登录失效！");
		}
		
		MerchantSettDetail obj = merchantSettDetailDaoImpl.getMerchantSettDetailForUpdate(entity.getMrchSettDetailId());
		if(obj == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE, "要修改的商户结算明细信息不存在！");
		}
		
		Merchant merchant = merchantDaoImpl.getMerchantForUpdate(obj.getMerchantNo());
		if(merchant == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "商户不存在！");
		}
		
		//修改商户保证金
		if(entity.getTranType().equals(MerchantSettDetail.MERCHANTSETTDETAIL_TRANTYPE_PREPAID)){
			if(obj.getTranType().equals(MerchantSettDetail.MERCHANTSETTDETAIL_TRANTYPE_PREPAID)){
				merchant.setPrepaidBalance(merchant.getPrepaidBalance().subtract(obj.getAmount()).add(entity.getAmount()));
			}else{
				merchant.setPrepaidBalance(merchant.getPrepaidBalance().add(obj.getAmount()).add(entity.getAmount()));
			}
		}else{
			if(obj.getTranType().equals(MerchantSettDetail.MERCHANTSETTDETAIL_TRANTYPE_PREPAID)){
				merchant.setPrepaidBalance(merchant.getPrepaidBalance().subtract(obj.getAmount()).subtract(entity.getAmount()));
			}else{
				merchant.setPrepaidBalance(merchant.getPrepaidBalance().add(obj.getAmount()).subtract(entity.getAmount()));
			}
		}
		
		obj.setPrepaidBalance(merchant.getPrepaidBalance());
		
		obj.setOperId(input.getOperId());
		
		//结算时间
		obj.setTranTime(commonDaoImpl.getDatabaseSysTime());
		//结算类型
		if(PCPSUtil.isEmpty(entity.getTranType())){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请选择结算类型！");
		}
		obj.setTranType(entity.getTranType());
		//结算资金
		if(entity.getAmount() == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请输入结算资金！");
		}
		obj.setAmount(entity.getAmount());
		//银行账户
		if(entity.getPaidBankAccountId() == null){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请选择付款银行账户！");
		}
		obj.setPaidBankAccountId(entity.getPaidBankAccountId());
		//凭证号
		if(PCPSUtil.isEmpty(entity.getVoucherNo())){
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.RC_ARGS_NULL, "请输入凭证号！");
		}
		obj.setVoucherNo(entity.getVoucherNo());
		obj.setRemark(entity.getRemark());
		
		//修改商户保证金
		merchantDaoImpl.updateMerchant(merchant);
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(input,
				SysOperLog.operType_Edit, "修改商户保证金", "merchantNo: " + merchant.getMerchantNo() + ";GuaranteeAmount:" + merchant.getPrepaidBalance());
		
		merchantSettDetailDaoImpl.modifyMerchantSettDetail(obj);
		// 插入数据库操作日志
		sysOperLogDaoImpl.insertSysOperLogInfo(input,
				SysOperLog.operType_Edit, "修改商户结算明细", "merchantNo: " + obj.getMerchantNo() + ";tranType:" + obj.getTranType() + ";amount:" + obj.getAmount());
	}

}
